<?php
	class Users extends MY_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('users_model');
			$this->load->model('group_model');
			$this->load->model('assign_model');
			$this->load->model('module_model');
		}
		
		public function index($limit=FALSE){			
			//set the limit or offset for each page
			$this->users_model->pager($limit);
			
			// define the link for pagination
			$my_link = site_url('users');
			
			$data['users'] = $this->users_model->getUserList();
			$data['title'] = "User list";
			
			$this->pager($my_link, $this->users_model->get_total(), $this->users_model->get_record_per_page(),2);
				
			$this->load->view('templates/header1', $data);
			$this->load->view('users/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
			
		}	
		
		public function displayForm($id=FALSE){
			$this->load->library('form_validation');
			$this->load->helper("form");
			
			/* Captcha test */
			/*$this->load->helper('captcha');
			$vals = array(
			    'word' => 'Random word',
			    'img_path' => './captcha/',
			    'img_url' => 'http://example.com/captcha/',
			    'font_path' => './path/to/fonts/texb.ttf',
			    'img_width' => '150',
			    'img_height' => 30,
			    'expiration' => 7200
		    );

			$cap = create_captcha($vals);
			$data['cap'] = $cap;*/
			
			$data['group'] = $this->group_model->getGroupList();
			
			if($id!=FALSE){
				$data['users'] = $this->users_model->getUserList($id);	
				$data['title'] = "Edit user";			
				$this->load->view('templates/header1', $data);
				$this->load->view('users/form_edit');
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}			
			else{
				$data['title'] = "Create user";			
				$this->load->view('templates/header1', $data);
				$this->load->view('users/form');
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			
		}
		
		public function create(){
			$this->displayForm();
		}
		
		public function edit($id){
			$this->displayForm($id);
		}
		
		public function delete($id){
			$this->users_model->delete($id);
			redirect("users", "refresh");
		}
		
		public function change($id){
			$this->load->helper("form");
			$this->load->library('form_validation');
			$data['users'] = $this->users_model->getUserList($id);
			$data['title'] = "Change password";
			$this->load->view('templates/header1', $data);
			$this->load->view('users/form_password', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function save_password(){
			$this->load->helper("form");
			$this->load->library('form_validation');
			$this->form_validation->set_rules('password', 'Password', 'callback_password_check');
			if ($this->form_validation->run() === FALSE){
				$data['title'] = "Change password";
				$this->load->view('templates/header1', $data);
				$this->load->view('users/form_password', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$this->users_model->update_password();
				redirect("users", "refresh");
			}
		}
		
		public function save(){
			$flag_check = TRUE;
			$this->load->helper("form");
			$this->load->library('form_validation');
			$id = $this->input->post('id');
			$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
			$this->form_validation->set_rules('email', 'Email', 'callback_email_check');
			
			if(isset($id) && (empty($id) || $id==0)){
				$this->form_validation->set_rules('password', 'Password', 'callback_password_check');			
			}
				
			
			if ($this->form_validation->run() === FALSE){
				$data['group'] = $this->group_model->getGroupList();
				$data['title']="Create User";
				$data['users'] = $this->input->post();
				$this->load->view('templates/header1', $data);
				$this->load->view('users/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
					$this->users_model->save();
					redirect("users", "refresh");
			}
		}
		
		public function username_check($str){
			$str = trim($str);
			if(empty($str)){
				$this->form_validation->set_message('username_check', 'The username is required');
				return FALSE;
			}
			else{
				if($this->users_model->checkDuplicateUsername()===FALSE){
					$this->form_validation->set_message('username_check', 'The username already exists');
					return FALSE;
				}
				return TRUE;
			}
		}
		
		public function email_check($str){
			$str = trim($str);
			if(empty($str)){
				$this->form_validation->set_message('email_check', 'The email is required');
				return FALSE;
			}
			else{
				if($this->users_model->checkDuplicateEmail()===FALSE){
					$this->form_validation->set_message('email_check', 'The email already exists');
					return FALSE;
				}
				return TRUE;
			}
		}
		
		public function password_check($str){
			$str = trim($str);
			if(empty($str)){
				$this->form_validation->set_message('password_check', 'The password is required');
				return FALSE;
			}
			else{
				if($this->users_model->checkPassword()===FALSE){
					$this->form_validation->set_message('password_check', 'The password does not match');
					return FALSE;
				}
				return TRUE;
			}
		}
		
	}
?>